import Request from 'utils/request';

import AddressForm from 'models/goodsPayment/addressForm';
import PayOrder from 'models/goodsPayment/payOrder';

export default class GoodsPaymentApi {
  static getGoodsAddressList() {
    const apiName = 'api/wehcatshop/getWeChatUserInfoAddress';

    return Request.post(apiName).then(res => {
      let addressData = res.data;
      let addressList = [];
      let defaultAddress = null;
      for (let i = 0; i < addressData.length; i++) {
        let address = addressData[i];
        address = new AddressForm(address);
        addressList.push(address);
        if (address.isDefault) {
          defaultAddress = address;
        }
      }

      return {
        addressList,
        defaultAddress
      };
    });
  }

  static getAddressById(id, hasObj = true) {
    const apiName = 'api/wehcatshop/getSingleWeChatUserInfoAddress';

    return Request.post(apiName, {
      weChatUserInfoAddressId: id
    }).then(res => {
      let address = res.data;
      if (hasObj) {
        address = new AddressForm(address);
      }

      return address;
    });
  }

  static addAddress(addressForm) {
    const apiName = 'api/wehcatshop/addWeChatUserInfoAddress';
    let postData = addressForm.getObject('add');

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updateAddress(addressForm) {
    const apiName = 'api/wehcatshop/updateWeChatUserInfoAddress';
    let postData = addressForm.getObject('update');

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static deleteAddress(id) {
    const apiName = 'api/wehcatshop/updateWeChatUserInfoAddress';

    return Request.post(apiName, {
      weChatUserInfoAddressId: id,
      status: 0
    }).then(res => {
      return res.msg;
    });
  }

  static changeDefault(id) {
    const apiName = 'api/wehcatshop/updateWeChatUserInfoAddressSetDefault';

    return Request.post(apiName, {
      weChatUserInfoAddressId: id,
      defaultAddress: 1
    }).then(res => {
      return res.msg;
    });
  }

  static payGoodsList(goodsList, lnglat, type) {
    const apiName = 'wechatpay/payForMutiProductAndBuyCar';
    for (let i = 0; i < goodsList.length; i++) {
      const goods = goodsList[i];
      goods.source = "phone_wxpay";
      goods.sourceSub = type;
      goods.tradeType = "JSAPI";
      goods.longitude = lnglat.lng;
      goods.latitude = lnglat.lat;
    }

    return Request.post(apiName, {
      queryParamsPayInfos: goodsList
    }).then(res => {
      let data = res.data;
      return data[0].mainOrderNo;
    });
  }

  static payActivityGoodsList(goods, lnglat, type) {
    const apiName = 'wechatpay/payForMutiProductAndBuyCar';

    goods.source = "phone_wxpay";
    goods.sourceSub = type;
    goods.tradeType = "JSAPI";
    goods.longitude = lnglat.lng;
    goods.latitude = lnglat.lat;

    return Request.post(apiName, {
      queryParamsPayInfos: [goods]
    }).then(res => {
      let data = res.data;
      return data[0].mainOrderNo;
    });
  }

  static payPrice(postData, lnglat, type) {
    const apiName = 'wechatpay/payForNoGoodsOrder';
    postData.longitude = lnglat.lng;
    postData.latitude = lnglat.lat;
    postData.source = "phone_wxpay";
    postData.tradeType = "JSAPI";
    postData.sourceSub = type;

    return Request.post(apiName, postData).then(res => {
      let data = res.data;
      return {
        timeStamp: data.timestamp,
        nonceStr: data.noncestr,
        package: data.packageStr,
        signType: data.signType,
        paySign: data.sign
      }
    });
  }

  static getOrderById(orderId) {
    const apiName = 'wechatpay/getGoodsInfoByMainOrderNo';

    return Request.post(apiName, {
      mainOrderId: orderId
    }).then(res => {
      let data = res.data;
      let order = new PayOrder(data, orderId);
      return order;
    });
  }

  static updateOrderAddress(order) {
    const apiName = 'wechatpay/updateOrderInfoByMainOrderNo';

    return Request.post(apiName, {
      mainOrderId: order.orderId,
      buyerName: order.buyerName,
      buyerPhone: order.buyerPhone,
      buyerAddress: order.buyerAddress
    }).then(res => {
      return res.msg;
    });
  }

  static getPayData(orderId) {
    const apiName = 'wechatpay/payForPreParOrder';

    return Request.post(apiName, {
      queryParamsPayInfos: [{
        mainOrderId: orderId
      }]
    }).then(res => {
      let data = res.data[0];
      return {
        timeStamp: data.timestamp,
        nonceStr: data.noncestr,
        package: data.packageStr,
        signType: data.signType,
        paySign: data.sign
      };
    });
  }

  static getAdvertise() {
    const apiName = 'api/wehcatshop/getAdvertisement';

    return Request.post(apiName, {
      advertisementStatus: 1
    }).then(res => {
      let data = res.data;
      let advertise = null;
      for (let i = 0; i < data.length; i++) {
        const obj = data[i];
        let type = obj.advertisementType;
        if (type !== 3) {
          continue;
        }
        advertise = {
          type,
          id: obj.advertisementId + '',
          href: obj.advertisementHref,
          name: obj.advertisementName,
          sort: obj.advertisementSort,
          imgUrl: obj.advertisementUrl
        };
        break;
      }
      return advertise;
    });
  }
}
